package yang.yu.sunny.domain.sales;

import org.dayatang.domain.InstanceFactory;
import yang.yu.sunny.domain.AbstractEntity;
import yang.yu.sunny.domain.product.Product;
import yang.yu.sunny.infra.persistence.product.ProductMapper;
import yang.yu.sunny.infra.persistence.sales.ProductPriceMapper;

import java.util.Objects;

/**
 * 订单项目
 * Created by yyang on 16/3/15.
 */
public class OrderItem extends AbstractEntity {

    private Long orderId;

    private Long productId;

    private String productSn;

    private Long categoryId;

    private double unitPrice;

    private double quantity;

    private double totalPrice;

    private double discount;

    public static OrderItemBuilder builder() {
        return new OrderItemBuilder();
    }

    protected OrderItem() {
    }

    public Long getOrderId() {
        return orderId;
    }

    public Long getProductId() {
        return productId;
    }

    public String getProductSn() {
        return productSn;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public double getQuantity() {
        return quantity;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof OrderItem)) {
            return false;
        }
        OrderItem that = (OrderItem) other;
        return Objects.equals(this.getOrderId(), that.getOrderId()) &&
                Objects.equals(this.getProductId(), that.getProductId());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getOrderId(), getProductId());
    }

    public static class OrderItemBuilder {

        private Order order;

        private Product product;

        private double quantity;

        private double discount;

        public OrderItemBuilder order(Order order) {
            this.order = order;
            return this;
        }

        public OrderItemBuilder product(Product product) {
            this.product = product;
            return this;
        }

        public OrderItemBuilder productId(Long productId) {
            ProductMapper productMapper = InstanceFactory.getInstance(ProductMapper.class);
            this.product = productMapper.get(productId);
            return this;
        }

        public OrderItemBuilder quantity(double quantity) {
            this.quantity = quantity;
            return this;
        }

        public OrderItemBuilder discount(double discount) {
            this.discount = discount;
            return this;
        }

        public OrderItem build() {
            OrderItem item = new OrderItem();
            item.orderId = order.getId();
            item.productId = product.getId();
            item.productSn = product.getSn();
            item.categoryId = product.getCategoryId();
            item.quantity = quantity;
            item.discount = discount;
            ProductPriceMapper mapper = InstanceFactory.getInstance(ProductPriceMapper.class);
            item.unitPrice = mapper.getUnitPriceOf(item.productId, order.getCreated());
            item.totalPrice = item.unitPrice * quantity * (1 - discount);
            return item;
        }
    }
}
